package com.bio.action.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.bio.action.AbstractAction;
import com.bio.entity.user.Company;
import com.bio.entity.user.Position;
import com.bio.service.user.CompanyService;
import com.bio.service.user.PositionService;
import com.bio.utils.Environment;

@Namespace("/position")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/position/listPositions.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "position!listByCompany",params={"companyId","${companyId}"}, type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/position/editPosition.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/position/addPosition.jsp", type = "dispatcher")
		})
public class PositionAction extends AbstractAction<Position, PositionService> {

	private static final long serialVersionUID = -8211098267731542109L;

	public PositionAction(PositionService service) {
		super(service);
	}

	Logger logger = Logger.getLogger(CompanyAction.class);
	
	private Position position;
	
	private Long companyId;
	
	private String json;
	
	private List<Position> allPositions;
	
	private CompanyService companyService;
	
	public String list(){
		pagination.setCurrentIndex(this.getCurrentIndex());
		this.pager = this.service.findAllByPager(pagination);
		return "list";
	}
	
	public String listByCompany(){
		pagination.setCurrentIndex(this.getCurrentIndex());
		this.companyId = Environment.Session.getCompanyId();
		this.pager = this.service.findPagerByCompanyId(pagination, companyId);
		return "list";
	}
	
	public String add(){
		companyId = Environment.Session.getCompanyId();
		this.allPositions = this.service.findAllByCompanyId(this.companyId);
		return "add";
	}
	
	public String save(){
		Calendar now = Calendar.getInstance();
		this.position.setCreatedDate(now);
		
		//添加公司信息
		Company company = this.companyService.get(this.position.getCompanyId());
		this.position.setCompanyName(company.getName());
		
		//添加上级信息
		if(null != this.position.getSuperiorId()){
		    Position superior = this.service.get(this.position.getSuperiorId());
		    this.position.setSuperiorName(superior.getName());
		    this.position.setSuperiorNo(superior.getPositionNo());
		}
		
		this.service.save(this.position);
		this.companyId = this.position.getCompanyId();
		return "tolist";
	}
	
	public String update(){
		Position oldPosition = this.service.get(position.getId());
		
		this.position.setUpdatedDate(Calendar.getInstance());
		this.position.setCreatedDate(oldPosition.getCreatedDate());
		
		if(null != this.position.getSuperiorId()){
		    Position superior = this.service.get(this.position.getSuperiorId());
		    this.position.setSuperiorName(superior.getName());
		    this.position.setSuperiorNo(superior.getPositionNo());
		}
		this.service.save(position);
		return "tolist";
	}
	
	public String edit(){
		this.position = this.service.get(id);
		this.allPositions = this.service.findAllByCompanyId(this.position.getCompanyId());
		return "edit";
	}
	
	public String delete(){
		this.companyId = this.service.get(id).getCompanyId();
		this.service.remove(id);
		return "tolist";
	}
	
	public void getPositionsByCompanyId() throws IOException{
		allPositions = this.service.findAllByCompanyId(companyId);
//		JSONArray rawdata = new JSONArray();
//        JSONObject obj = new JSONObject();
//        for(Position p : allPositions){
//        	JSONArray pdata = new JSONArray();
//        	obj.accumulate("pid", p.getId());
//        	obj.accumulate("pname", p.getName());
//        	pdata.add(obj);
//        	rawdata.add(pdata);
//        }
//        obj.accumulate("data", rawdata);
		this.json = "";
		for(Position p : allPositions){
			this.json += p.getId() + "," + p.getName() + ";";
		}
		if(this.json.length() > 0){
		    this.json = this.json.substring(0, this.json.length()-1);
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		//response.setContentType("text/plain");
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		out.flush();
		out.close();
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public Long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}

	public List<Position> getAllPositions() {
		return allPositions;
	}

	public void setAllPositions(List<Position> allPositions) {
		this.allPositions = allPositions;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

}
